import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {environment} from "../../../environments/environment";
import {NewItem} from "../../modals/basic.modal";
import {BasicService} from "../../unit/basic-service";

@Component({
    selector: 'app-news',
    templateUrl: './news.page.html',
    styleUrls: ['./news.page.scss'],
})
export class NewsPage implements OnInit {
    currentTitle: string;
    newList: Array<NewItem>;

    constructor(
        private route: Router,
        private basicService: BasicService,
        private activateInfo: ActivatedRoute,
    ) {
    }

    ngOnInit() {
        this.newList = [];
        this.activateInfo.queryParamMap.subscribe((res: any) => {
            if (res?.params?.type) {
                this.getNews(res.params.type);
            }
        });
    }

    getNews(type) {
        this.currentTitle = type === 'theme' ? '专题活动' : "热点新闻";
        const url = type === 'theme' ? 'ztxw' : 'rdxw';
        this.basicService.getData(`appMessage/${url}`).subscribe(res => {
            this.reGroupNews(res);
        });
    }

    reGroupNews(data) {
        const token = localStorage.getItem('medicalToken'); // token保存在localstorage
        data.map(item => {
            item.url = `${environment.basicUrl}/uploadappmessage/${item.filepath}?access_token=${token}`;
        });
        this.newList = [...data];
    }

    goToPage(id) {
        this.route.navigate([`/new-detail`], {queryParams: {id}}).then();
    }

}
